package io.terminus.paas.profiler.restful

import io.terminus.paas.profiler.common.BaseController
import io.terminus.paas.profiler.model.TracingRecord
import io.terminus.paas.profiler.service.MonitorService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

/**
 * Created by gengrong on 16/4/5.
 */
@RestController
@RequestMapping("/api/services/{serviceId:.*}/tracings")
class Tracings @Autowired constructor(
        val monitorService: MonitorService
) : BaseController() {

    @RequestMapping(method = arrayOf(RequestMethod.GET))
    fun tracingList(@PathVariable serviceId: String): List<TracingRecord> {
        return monitorService.findByServiceId(getRegistryAddress(), serviceId);
    }

    @RequestMapping(method = arrayOf(RequestMethod.DELETE))
    fun batchDelete(@PathVariable serviceId: String, @RequestBody rootIds: List<String>) {
        rootIds.forEach { rootId ->
            monitorService.deleteByRootId(getRegistryAddress(), serviceId, rootId);
        }
    }

    @RequestMapping("/{rootId}", method = arrayOf(RequestMethod.GET))
    fun tracingDetail(@PathVariable serviceId: String, @PathVariable rootId: String): List<TracingRecord> {
        return monitorService.findByServiceIdAndRootId(getRegistryAddress(), serviceId, rootId);
    }

    @RequestMapping("/{rootId}", method = arrayOf(RequestMethod.DELETE))
    fun delete(@PathVariable serviceId: String, @PathVariable rootId: String) {
        monitorService.deleteByRootId(getRegistryAddress(), serviceId, rootId);
    }

}